<template>
  <!-- 封装的设备类型管理的新增组件 -->
  <div>
    <el-dialog
      title="新增商品"
      :visible="visible"
      width="30%"
      @close="$emit('addShowFn')"
    >
      <el-form ref="form" :model="iptValue" :rules="form" label-width="80px">
        <el-form-item label="商品名称:" prop="skuName" label-width="100px">
          <el-input v-model="iptValue.skuName" />
        </el-form-item>
        <el-form-item label="品牌:" prop="brandName" label-width="100px">
          <el-input v-model="iptValue.brandName" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price" label-width="100px">
          <el-input-number
            v-model="iptValue.price"
            style="width:430px"
            controls-position="right"
            :min="1"
            :max="10"
          />
        </el-form-item>
        <el-form-item label="商品类型" prop="classId" label-width="100px">
          <el-select v-model="iptValue.classId" placeholder="请选择活动区域" style="width:430px">
            <el-option v-for="item in goodsList" :key="item.classId" :label="item.className" :value="item.classId" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格:" prop="unit" label-width="100px">
          <el-input v-model="iptValue.unit" />
        </el-form-item>
        <el-form-item label="设备图片:" prop="skuImage" label-width="100px">
          <div
            action="#"
            class="avatar-uploader"
            @click="$refs.iptFile.click()"
          >
            <img v-if="iptValue.skuImage" class="pic" :src="iptValue.skuImage" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <input
              v-show="false"
              ref="iptFile"
              type="file"
              accept="image/*"
              @change="onFileChange"
            >
          </div>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('addShowFn')">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { modifyGoods, goodsTypeSearch, picApi, addGoods } from '@/api'
// import { AddTactics } from '@/api'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      row: '',
      goodsList: [], // 商品类型列表
      iptValue: {
        skuName: '', // 名称
        brandName: '', // 品牌
        price: '', // 价格
        classId: '', // 类型
        unit: '', // 规格
        skuImage: '' // 图片
      },
      form: {
        skuName: {
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        },
        brandName: {
          required: true,
          message: '请输入品牌',
          trigger: 'blur'
        },
        price: {
          required: true,
          message: '请输入价格',
          trigger: 'blur'
        },
        classId: {
          required: true,
          message: '请选择类型',
          trigger: 'blur'
        },
        unit: {
          required: true,
          message: '请输入规格',
          trigger: 'blur'
        },
        skuImage: {
          required: true,
          message: '请放入图片',
          trigger: 'blur'
        }
      }
    }
  },
  watch: {
    tow() {
      console.log(this.row)
    }
  },
  async created() {
    const { data } = await goodsTypeSearch(1, 10000)
    console.log(data)
    this.goodsList = data.currentPageRecords
  },
  methods: {
    modifyFn(row) {
      this.row = row
      this.iptValue = {
        skuName: row.skuName, // 名称
        brandName: row.brandName, // 品牌
        price: row.price, // 价格
        classId: row.classId, // 类型
        unit: row.unit, // 规格
        skuImage: row.skuImage // 图片
      }
    },
    async addSubmit() {
    // 确认
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.row.brandName) {
          console.log(this.row)
          await modifyGoods(this.row.skuId, this.iptValue)
          this.$message.success('修改成功')
          this.$emit('addShowFn')
        } else {
          try {
            await addGoods(this.iptValue)
            this.$message.success('添加成功')
            this.$emit('addShowFn')
            this.$emit('publicFn')
          } catch (error) {
            this.$message.success('失败')
            console.log(error)
          }
        }
      })
    },
    // 文件选择方法
    async onFileChange(ev) {
      //   console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('fileName', ev.target.files[0]) // photo在表单里参数名携带
      const res = await picApi(fd)
      console.log(res)
      // console.log(res.data.data.photo)
      this.iptValue.skuImage = res.data
    }

  }

}

</script>

<style lang="scss" scoped>
.cli {
  background-color: #f3f6fb;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.pic{
  width: 76px;
  height: 76px;
}
</style>
